import '../../public.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  int count = 0;

  void add() {
    setState(() {
      count++;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    add();
    print(count);
  }

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

//  HideAlways
  @override
  Widget build(BuildContext context) {
    super.build(context);
    // TODO: implement build
    return ProviderWidget<HomeModel>(
        model: HomeModel(Provider.of<ThemeModel>(context)),
        onModelReady: (model) async => model.initData(),
        builder: (context, model, child) {
          if (model.busy) {
            return ViewStateBusyWidget();
          } else if (model.error) {
            return ViewStateWidget(onPressed: model.initData);
          } else if (model.empty) {
            return ViewStateEmptyWidget(onPressed: model.initData);
          }
          return Container(
              color: Color(0xFF1f1f1f),
              child: SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: MaterialClassicHeader(
                  color: Colors.white,
                  backgroundColor: Colors.black54,
                ),
                footer: CustomFooter(
                  loadStyle: LoadStyle.ShowWhenLoading,
                  height: 60,
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text(
                        "上拉加载",
                        style: TextStyle(
                          color: model.themeModel.currentBrightness ==
                                  Brightness.dark
                              ? Color(0xFF666666)
                              : Color(0xFF444444),
                        ),
                      );
                    } else if (mode == LoadStatus.loading) {
                      body = ViewStateLoadingWidget(
                        color: model.themeModel.currentBrightness ==
                                Brightness.dark
                            ? Color(0xFF666666)
                            : Color(0xFF444444),
                      );
                    } else if (mode == LoadStatus.failed) {
                      body = Text("加载失败!",
                          style: TextStyle(
                            color: model.themeModel.currentBrightness ==
                                    Brightness.dark
                                ? Color(0xFF666666)
                                : Color(0xFF444444),
                          ));
                    } else if (mode == LoadStatus.canLoading) {
                      body = Text("松手,加载更多!",
                          style: TextStyle(
                            color: model.themeModel.currentBrightness ==
                                    Brightness.dark
                                ? Color(0xFF666666)
                                : Color(0xFF444444),
                          ));
                    } else {
                      body = Text("没有更多数据了!",
                          style: TextStyle(
                            color: model.themeModel.currentBrightness ==
                                    Brightness.dark
                                ? Color(0xFF666666)
                                : Color(0xFF444444),
                          ));
                    }
                    return Container(
                      height: 55.0,
                      color: Color(0xFF1f1f1f),
                      child: Center(child: body),
                    );
                  },
                ),
                controller: _refreshController,
                onRefresh: () async {
                  //我在下拉刷新时执行
                  await model.refresh();
                  // if failed,use refreshFailed()
                  _refreshController.refreshCompleted();
                },
                onLoading: () async {
                  //我在上滑加载更多时执行
                  await model.loadMore();
                  _refreshController.loadComplete();
                },
                child: ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
//                return Card(child: Center(child: Text(model.list[index].title)));
                    return MomentsItem(item: model.list[index], model: model);
                  },
                  itemCount: model.list.length,
                  shrinkWrap: true,
                ),
              ));
        });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
